package com.wicket.okrcomponent.biz.service.nb;

import cn.hutool.core.bean.BeanUtil;
import com.wicket.okrcomponent.biz.service.dto.res.ImplementSmartSessionReceiveFieldInputRespDto;
import com.wicket.okrcomponent.common.util.CommonFunctionHelper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import com.wicket.okrcomponent.biz.service.dto.req.DeleteFileReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.DeleteFileRespDto;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.wicket.okrcomponent.biz.service.dto.req.ImplementTenReceivingFieldReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.ImplementTenReceivingFieldRespDto;
import com.wicket.okrcomponent.biz.service.dto.req.AnalysisLevelCodeReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.AnalysisLevelCodeRespDto;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
public class NbCommon {


    private boolean isStartWithHttp(String str) {
        if (str != null && str.startsWith("http")) {
            // 去掉域名
            int index = str.indexOf("//");
            if (index != -1) {
                index = str.indexOf("/", index + 2);
                if (index != -1) {
                    str = str.substring(index);
                } else {
                    str = "/";
                }
            }
            return true;
        }
        return false;
    }
/**
      * code:deleteFile
      * name:M2删除附件(特殊方法）
      * desc:undefined
      * gen by moon at 9/25/2023, 6:34:50 PM
      * 
      **/
     @Trace(operationName = "M2删除附件(特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public DeleteFileRespDto deleteFile(DeleteFileReqDto reqDto) {
         String path = reqDto.getMediaContentAddress();
         System.out.println("src path : "+reqDto.getMediaContentAddress()+"\r\n");
         isStartWithHttp(path);
         path = "/www/server" + path;
         File file = new File(path);
         System.out.println("deal path : " + path + "\r\n");
         if (file.exists()) {
             System.out.println("hit path\r\n");
             file.delete();
         }
        return new DeleteFileRespDto();
      }
/**
      * code:receptionService
      * name:M2接收排序值出参
      * desc:undefined
      * gen by moon at 7/23/2024, 8:14:00 PM
      *
      **/
     @Trace(operationName = "M2接收排序值出参")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ImplementTenReceivingFieldRespDto implementTenReceivingField(ImplementTenReceivingFieldReqDto reqDto) {
         return BeanUtil.toBean(reqDto, ImplementTenReceivingFieldRespDto.class);
      }
/**
      * code:getCurrentContentCode
      * name:M2分析层级标识（特殊方法）
      * desc:undefined
      * gen by moon at 7/23/2024, 8:14:18 PM
      *
      **/
     @Trace(operationName = "M2分析层级标识（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public AnalysisLevelCodeRespDto analysisLevelCode(AnalysisLevelCodeReqDto reqDto) {
         AnalysisLevelCodeRespDto retData = new AnalysisLevelCodeRespDto();
         if (reqDto.getSiblingCodeList() == null || reqDto.getSiblingCodeList().size() == 0) {
             if(reqDto.getSuperiorContentCode().equals("1")){
                 retData.setCurrentContentCode(reqDto.getSuperiorContentCode() + "001");
             }else{
                 retData.setCurrentContentCode(reqDto.getSuperiorContentCode() + "1001");
             }

         } else if (reqDto.getSiblingCodeList().size() > 0) {
             String first = reqDto.getSiblingCodeList().get(0);
             String preFix = first.substring(0,first.length()-4);
             List<String> shortCodeList = reqDto.getSiblingCodeList().stream().map(code -> code.substring(code.length() - 4)).collect(Collectors.toList());
             List<Long> codeNumList = shortCodeList.stream().map(code -> Long.valueOf(code)).collect(Collectors.toList());
             Long max = Collections.max(codeNumList) + 1;
             retData.setCurrentContentCode(preFix + String.valueOf(max));
         }
         return retData;
      }
  //手工接入方法
}
